package com.rfsp.common.util;

import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;



public class XMLUtil {
	private static final Logger logger = LoggerFactory
			.getLogger(XMLUtil.class);
	public static String string2XML(String xmlString) {
		
		   String dataSource = xmlString.substring(xmlString.indexOf("data-source")+13,xmlString.indexOf("data-template")-2);
		   String datatemplate = xmlString.substring(xmlString.indexOf("<li>"),xmlString.indexOf("</li>")+5);
		   
		   String prifix = xmlString.substring(0,xmlString.indexOf("<li>"));
		   String suffix = xmlString.substring(xmlString.indexOf("</ul>"));
		   
		   String begin = xmlString.substring(0,xmlString.indexOf("</ul>"));
		   String beginresultprifix = begin.substring(0, begin.indexOf("data-template"));
		   String endresultprifix = begin.substring(begin.length()-1);
		   String resultPrifix = beginresultprifix + endresultprifix;

		   logger.debug(prifix);
		   logger.debug(suffix);

		   
		   Map<String,Object> map1 = new  HashMap<String,Object>();
		   Map<String,Object> map2 = new  HashMap<String,Object>();
		   map1.put("username", "username1");
		   map1.put("fullName", "fullName1");
		   map1.put("regDateTime", "regDateTime1");
		   map2.put("username", "username2");
		   map2.put("fullName", "fullName2");
		   map2.put("regDateTime", "regDateTime2");  
		   
		   
		   List<Map<String,Object>> userList = new ArrayList<Map<String,Object>>();
		   userList.add(map2);
		   userList.add(map1);
		   Map<String,Object> viewData=new HashMap<String,Object>();
		   
		   
		   viewData.put("userList",userList);
		   
		   List list = (ArrayList)viewData.get(dataSource);
		   
		   StringBuffer resultString = new StringBuffer();
		   resultString.append(resultPrifix);
		   for(int i=0 ; i<list.size(); i++) {
			   
			   Map<String,Object> singlmap = new HashMap<String,Object>();
			   singlmap = (HashMap<String,Object>) list.get(i);
			   String singleString = datatemplate;
			   String username = (String)singlmap.get("username");
			   String fullName = (String)singlmap.get("fullName");
			   String regDateTime = (String)singlmap.get("regDateTime");
			   
			   singleString =  singleString.replaceAll("<username>",  username);
			   singleString =  singleString.replaceAll("<fullName>",  fullName);
			   singleString =  singleString.replaceAll("<regDateTime>",  regDateTime);
			   
			   resultString.append(singleString);
		   }
		   resultString.append(suffix);
		
		   return resultString.toString();
	}
	  
	
	public String string2XML1(String  ss1) {
		
		return null;
	}

	
	 public static String beanToXML(Object obj) {
	        try {
	            JAXBContext context = JAXBContext.newInstance(obj.getClass());

	            Marshaller marshaller = context.createMarshaller();
	            marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");// //编码格式
	            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false);// 是否格式化生成的xml串
	            marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);// 是否省略xm头声明信息
	            StringWriter writer = new StringWriter();
	            marshaller.marshal(obj, writer);
	            return writer.toString();
	        } catch (Exception e) {
	            throw new RuntimeException(e);
	        }
	    }

	    @SuppressWarnings("unchecked")
	    public static <T> T XmlToBean(String xml, Class<T> valueType) {
	        try {
	            JAXBContext context = JAXBContext.newInstance(valueType);
	            Unmarshaller unmarshaller = context.createUnmarshaller();
	            return (T) unmarshaller.unmarshal(new StringReader(xml));
	        } catch (Exception e) {
	            throw new RuntimeException(e.getMessage());
	        }
	    }


   public static void main(String[] args) {
	   
	   
	   String xmlString = "<ul runat=\"server\" class=\"userList\" data-source=\"userList\" data-template=\"<li>用户名：<a href='#' class='username'><username></a> 姓名：<a href='#' class='fullName'><fullName></a>　注册日期<span class='regDateTime'><regDateTime></span></li>\"></ul>";
	  
	   string2XML(xmlString);
	   

   }

}